package westh.ilib.struts.action.myTagCloud;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.config.Config;
import westh.ilib.service.tagClouds.TagCloudsService;
import westh.log.YyError;

/**
 * @author ChaiLinglu
 * 
 * @ModifyDate 2010-09-14
 * @modifier ChaiLinglu
 * @content refine
 * 
 */
public class SaveTagInfoAction extends Action {
	private final static Log log = LogFactory.getLog(SaveTagInfoAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
		DynaActionForm f = (DynaActionForm) form;
		//get data from page
		String userId=request.getRemoteUser();
		String rscId=request.getParameter("rscId");
		String tags=request.getParameter("tags");
		String rscDes = f.getString("comment");
		String score = f.getString("rating");
		String readStatus = f.getString("readStatus");
		String rscType=f.getString("rscType");
		
		String redirect = "../rscInfo.do?rscId=" + rscId;//skip forward
		//create input json for tagsysCloud
		JSONObject resultObject = new JSONObject();
		
		userId="tagCloudUser";////////////////////////////////////////////////tempTest
		
		resultObject.put("userId", userId);
		resultObject.put("rscId", rscId);
		resultObject.put("rscType", rscType);
		resultObject.put("rscDes", rscDes);
		resultObject.put("score", score);
		resultObject.put("tags", tags);
		resultObject.put("readStatus", readStatus);
		String inputStr = resultObject.toString();
		log.debug("saveTagInfo:INPUT:TAgsysCLoud:"+inputStr);
		//call WebService  -- tagsysCloud
		String outString = "";
		try {
			MuleClient clientD = new MuleClient();
			UMOMessage umo = clientD.send("axis:http://"+remoteUrl+":4481/DataCenter/TagDataCenter?method=saveTagInfo",inputStr, null);
			outString = (String) umo.getPayload();
			log.debug("saveTagInfo:outPut :TagsysCloud "+outString);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		
		String retCode=""; //return code
		String retMsg="";//return message
		String returnStr="";//

		JSONObject jsonOut= JSONObject.fromObject(outString);
		retCode = jsonOut.getString("retCode");
		if (retCode.equals("5500")) {
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			
			//create input json for ULASCloud
			JSONObject json=new JSONObject();
			JSONObject jsonObj=new JSONObject();
			jsonObj.put("userId", userId);
			jsonObj.put("time", TagCloudsService.getCurrentTime());
			jsonObj.put("rscId", rscId);
			String doing=TagCloudsService.getSaveTagStr(tags,score,readStatus);
			jsonObj.put("doing", doing);
			json.put("methodName", "saveTagLog");//rules control
			json.put("jsonStr", jsonObj);//rules control
			
			returnStr = "succ";
			//call ULASCloud
			westh.log.YyLog.rulesSave(request,json.toString());
		} else {
			YyError.show(retCode);
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", "标注失败！");
			request.setAttribute("redirect", redirect);
			returnStr = "fail";
		}
		request.setAttribute("info", "标注成功！");
		request.setAttribute("redirect", redirect);
		return mapping.findForward(returnStr);
	}
}
